<template>
  <div class="home-container">官方参考价格	供应商报价
    <el-row :gutter="20">
      <!-- 当日采购统计 -->
      <el-col :span="12">
        <el-card class="statistics-card">
          <div slot="header" class="card-header">
            <span class="card-title">当日计划采购总价</span>
            <el-tag type="primary">{{ todayDate }}</el-tag>
          </div>
          <div class="statistics-content">
            <div class="stat-item">
              <div class="stat-label">官方参考价格总价</div>
              <div class="stat-value">¥{{ statistics.today.total_base_amount }}</div>
            </div>
            <div class="stat-item">
              <div class="stat-label">供应商报价总价</div>
              <div class="stat-value">¥{{ statistics.today.total_supplier_quoted_amount }}</div>
            </div>
            <div class="stat-item">
              <div class="stat-label">实际采购总价</div>
              <div class="stat-value">¥{{ statistics.today.total_actual_amount }}</div>
            </div>
          </div>
        </el-card>
      </el-col>

      <!-- 本月采购统计 -->
      <el-col :span="12">
        <el-card class="statistics-card">
          <div slot="header" class="card-header">
            <span class="card-title">本月累计采购总价</span>
            <el-tag type="success">{{ monthDate }}</el-tag>
          </div>
          <div class="statistics-content">
            <div class="stat-item">
              <div class="stat-label">官方参考价格总价</div>
              <div class="stat-value">¥{{ statistics.month.total_base_amount }}</div>
            </div>
            <div class="stat-item">
              <div class="stat-label">供应商报价总价</div>
              <div class="stat-value">¥{{ statistics.month.total_supplier_quoted_amount }}</div>
            </div>
            <div class="stat-item">
              <div class="stat-label">实际采购总价</div>
              <div class="stat-value">¥{{ statistics.month.total_actual_amount }}</div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 详细统计表格 -->
    <el-row :gutter="20" style="margin-top: 20px;">
      <el-col :span="24">
        <el-card>
          <div slot="header" class="card-header">
            <span class="card-title">采购数据详细统计</span>
            <el-button type="primary" size="small" @click="refreshData">刷新数据</el-button>
          </div>
          <el-table :data="tableData" border style="width: 100%">
            <el-table-column prop="period" label="统计周期" width="120"></el-table-column>
            <el-table-column prop="base_amount" label="官方参考价格总金额" width="180">
              <template slot-scope="scope">
                <span class="price-text">¥{{ scope.row.base_amount }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="supplier_quoted_amount" label="供应商报价总金额" width="180">
              <template slot-scope="scope">
                <span class="price-text">¥{{ scope.row.supplier_quoted_amount }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="actual_amount" label="实际采购总金额" width="180">
              <template slot-scope="scope">
                <span class="price-text">¥{{ scope.row.actual_amount }}</span>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { http } from '@/api'

export default {
  name: "home",
  data() {
    return {
      statistics: {
        today: {
          total_base_price: '0.00',
          total_supplier_quoted_price: '0.00',
          total_actual_price: '0.00',
          total_base_amount: '0.00',
          total_supplier_quoted_amount: '0.00',
          total_actual_amount: '0.00'
        },
        month: {
          total_base_price: '0.00',
          total_supplier_quoted_price: '0.00',
          total_actual_price: '0.00',
          total_base_amount: '0.00',
          total_supplier_quoted_amount: '0.00',
          total_actual_amount: '0.00'
        }
      },
      todayDate: '',
      monthDate: ''
    }
  },
  computed: {
    tableData() {
      return [
        {
          period: '当日',
          base_price: this.statistics.today.total_base_price,
          supplier_quoted_price: this.statistics.today.total_supplier_quoted_price,
          actual_price: this.statistics.today.total_actual_price,
          base_amount: this.statistics.today.total_base_amount,
          supplier_quoted_amount: this.statistics.today.total_supplier_quoted_amount,
          actual_amount: this.statistics.today.total_actual_amount
        },
        {
          period: '本月',
          base_price: this.statistics.month.total_base_price,
          supplier_quoted_price: this.statistics.month.total_supplier_quoted_price,
          actual_price: this.statistics.month.total_actual_price,
          base_amount: this.statistics.month.total_base_amount,
          supplier_quoted_amount: this.statistics.month.total_supplier_quoted_amount,
          actual_amount: this.statistics.month.total_actual_amount
        }
      ]
    }
  },
  mounted() {
    this.initDates()
    this.loadStatistics()
  },
  methods: {
    initDates() {
      const today = new Date()
      const month = new Date()

      this.todayDate = today.toLocaleDateString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit'
      })

      this.monthDate = month.toLocaleDateString('zh-CN', {
        year: 'numeric',
        month: '2-digit'
      })
    },

    async loadStatistics() {
      try {
        const response = await http.dailyMenu.getPurchaseStatistics()
        if (response && response.data) {
          this.statistics = response.data
        }
      } catch (error) {
        console.error('加载统计数据失败:', error)
        this.$message.error('加载统计数据失败')
      }
    },

    refreshData() {
      this.loadStatistics()
      this.$message.success('数据已刷新')
    }
  }
}
</script>

<style scoped>
.home-container {
  padding: 20px;
}

.statistics-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-title {
  font-size: 16px;
  font-weight: bold;
  color: #303133;
}

.statistics-content {
  padding: 10px 0;
}

.stat-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #f0f0f0;
}

.stat-item:last-child {
  border-bottom: none;
}

.stat-label {
  color: #606266;
  font-size: 14px;
}

.stat-value {
  font-size: 18px;
  font-weight: bold;
  color: #409EFF;
}

.price-text {
  color: #409EFF;
  font-weight: bold;
}

.el-card {
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.el-card:hover {
  box-shadow: 0 4px 20px 0 rgba(0, 0, 0, 0.15);
}
</style>
